package net.haveraid.data;

import java.util.Iterator;
import java.util.List;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RaidCalendar implements Iterable<RaidDate>{
	
	private static final Logger logger = LoggerFactory.getLogger(RaidCalendar.class);

	private final DateTime start,end;
	private final List<Raid> raids;
	
	public RaidCalendar(DateTime start, DateTime end, List<Raid> raids) {
		this.start = start;
		this.end = end;
		this.raids = raids;
	}
	
	@Override
	public Iterator<RaidDate> iterator() {
		return new RaidCalendarIterator();
	}

	private class RaidCalendarIterator implements Iterator<RaidDate> {
		private DateMidnight current;
		
		public RaidCalendarIterator() {
			this.current = start.toDateMidnight();
		}

		@Override
		public boolean hasNext() {
			return current != null;
		}

		@Override
		public RaidDate next() {
			if(current == null) throw new IllegalStateException("No more elements in iterator");
			try {
				for(Raid r : raids) {
					if(r.getDate().toDateMidnight().equals(current)) {
						return new RaidDate(current, r);
					}
				}
				return new RaidDate(current, null);
			} finally {
				current = current.plusDays(1);
				if(current.compareTo(end) > 0) {
					current = null;
				}
			}
		}

		@Override
		public void remove() {
			
		}
	}
}
